package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;
import com.oracle.lingshangejia.view.TitleLayout;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddEvaluateActivity extends BaseActivity {


    @BindView(R.id.title)
    TitleLayout mTitle;
    @BindView(R.id.et_content)
    EditText mEtContent;
    private int mId;

    @Override
    public int setLayoutId() {
        return R.layout.activity_add_evaluate;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mId = getIntent().getIntExtra("id", 0);
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
                  save();
    }

    private void save(){
        String content = mEtContent.getText().toString();
        if (TextUtils.isEmpty(content)){
            MyToast.show("内容不能为空");
        }else {
            int ysid = (int) SpUtils.get(this, "ysid", 0);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("createBy", ysid);//当前登录用户id
            map.put("grId", mId);
            map.put("content", content);//居民id
            OkHttpUtils.getNetWork(this, Constant.evaluate, map, handler);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    Gson gson = new Gson();
                    String json = (String)msg.obj;
                    DataBean dataBean = gson.fromJson(json,DataBean.class );
                    if (dataBean.getCode() == 0){
                        MyToast.show("评估完成");
                        finish();
                    }else {
                        MyToast.show("");
                    }


                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(AddEvaluateActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(AddEvaluateActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(AddEvaluateActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

}
